package com.yyplatform.admin.platform.role.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.yyplatform.admin.platform.role.model.entity.SysLoginHistoryEntity;

/**
 * Created on 2024-09-14
 */
public interface SysLoginHistoryRepository extends JpaRepository<SysLoginHistoryEntity, Long>,
        JpaSpecificationExecutor<SysLoginHistoryEntity> {


    @Query(
            value =
                    "SELECT * FROM sys_login_history WHERE 1=1 "
                            + " AND if(:username != '' , `username` = :username, 1=1 )  "
                            + " AND if(:errCode != -1,  `error_code` = :errCode, 1=1 ) "
                            + " AND (create_time >= :beginTime AND create_time <= :endTime) ",
            countQuery =
                    "SELECT count(id) FROM sys_login_history WHERE 1=1 "
                            + " AND if(:username != '' , `username` = :username, 1=1 )  "
                            + " AND if(:errCode != -1,  `error_code` = :errCode, 1=1 ) "
                            + " AND (create_time >= :beginTime AND create_time <= :endTime )",
            nativeQuery = true)
    Page<SysLoginHistoryEntity> findPage(
            @Param("username") String username,
            @Param("errCode") Integer errCode,
            @Param("beginTime") Date beginTime,
            @Param("endTime") Date endTime,
            @Param("pageable") Pageable pageable);


}


